package com.example.springboot.demo1.web;

import com.example.springboot.demo1.SpringData.domain.User;
import com.example.springboot.demo1.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping(value = "/add")
    public String add(User user){

        return "user/add";
    }
    @RequestMapping(value = "/save")
    public String save(User user){
        userService.save(user);
        return "/user/list";
    }

    @RequestMapping("/list")
    public String listUI(HttpSession session){
        List<User> all = userService.findAll();
        session.setAttribute("users",all);
        return "user/list";
    }
}
